package com.wzapp.sitposture.setup.endpoint;

import com.alibaba.fastjson.JSONObject;
import com.wzapp.sitposture.common.base.BaseEndpoint;
import com.wzapp.sitposture.common.error.CustomizedException;
import com.wzapp.sitposture.common.restful.RestfulResult;
import com.wzapp.sitposture.common.util.MD5Util;
import com.wzapp.sitposture.common.util.MemDiskUtil;
import com.wzapp.sitposture.common.util.ToolUtil;
import com.wzapp.sitposture.setup.entity.City;
import com.wzapp.sitposture.setup.entity.Setup;
import com.wzapp.sitposture.setup.service.SetupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by qianglong on 2018/1/15.
 */
@Slf4j
@RestController
public class SetupEndpoint extends BaseEndpoint {

    @Autowired
    SetupService setupService;

    @Value("${ql.version}")
    private String defaultVersion;

    /**
     * 功能: <br/>
     *
     * @return org.springframework.http.ResponseEntity
     * @author ql
     * @create 2018/8/22 17:03
     **/
    @GetMapping("/setup/info")
    public ResponseEntity findSetUpInfo() throws CustomizedException {

        JSONObject result = setupService.findCityInfo();

        JSONObject memInfo = MemDiskUtil.getMemInfo();
        result.put("serverMemInfo", memInfo);
        result.put("serverTime", System.currentTimeMillis());
        return RestfulResult.ok(result);
    }

    /**
     * 功能: 一次性获取所有城市<br/>
     *
     * @param version 版本号
     * @param md5
     * @return org.springframework.http.ResponseEntity
     * @author ql
     * @create 2018/1/23 11:53
     **/
    @GetMapping("/setup/cities")
    public ResponseEntity findAllCity(String version, String md5) {

        version = initVersion(version);

        List<City> cities = setupService.findCitiesByVersion(version);
        if (ToolUtil.isNotEmpty(cities)) {

            String cityMD5 = MD5Util.encrypt(JSONObject.toJSON(cities).toString());
            if (log.isDebugEnabled()) {
                log.debug("-----------findAllCity cityMD5 is -------------- {}", cityMD5);
            }

            if (cityMD5.equals(md5)) {
                return RestfulResult.notModified(null);
            }
        }

        return RestfulResult.ok(cities);

    }

    /**
     * 功能: 获取省份列表<br/>
     *
     * @return java.util.List<City>
     * @author ql
     * @create 2018/4/10 15:31
     **/
    @GetMapping("/setup/cities/search/findProvince")
    public List<City> findProvince() {

        return setupService.findProvince();
    }

    /**
     * 功能: 获取省份下城市列表<br/>
     *
     * @param provinceCode 省份编码
     * @return java.util.List<City>
     * @author ql
     * @create 2018/4/10 15:31
     **/
    @GetMapping("/setup/cities/search/findByProvinceCode")
    public List<City> findByProvinceCode(String provinceCode) {

        return setupService.findCitiesByProvinceCode(provinceCode);
    }

    /**
     * 功能: 根据groupType实时获取配置信息<br/>
     *
     * @param groupType 分组类型
     * @param version   版本号
     * @return org.springframework.http.ResponseEntity
     * @author ql
     * @create 2018/1/22 16:57
     **/
    @GetMapping("/setup/setups/collect/{groupType}")
    public List<Setup> findSetupsByGroupTypeAndChannel(@PathVariable String groupType,
                                                       String version) throws CustomizedException {
        version = initVersion(version);

        List<Setup> setups = setupService.findByGroupType(groupType, version);

        return setups;
    }

    /**
     * web端无版本号时，获取默认最大版本号
     */
    private String initVersion(String version) {
        if (ToolUtil.isEmpty(version)) {
            return defaultVersion;
        }
        return version;
    }

    /**
     * 功能: 查询指定配置<br/>
     *
     * @author qianglong
     * @create 2019/1/11 10:28
     * @param id
     * @return com.eig.setup.entity.SetUp
     **/
    @GetMapping("/setup/setups/{id}")
    public Setup findOneById(@PathVariable("id") String id) {

        return setupService.findOneById(id);
    }

}
